import axios from "axios";
import WebUtils from "@/utils/webUtils";

const apiUrl = "http://localhost:8081";

export const getUserList = async (currentPage, pageSize) => {
    try {
        WebUtils.addToken();
        const response =
            await axios.get(`${apiUrl}/manage-user?page=` +
                currentPage + `&pageSize=` + pageSize);
        WebUtils.isRedirect(response.data.code);
        return response.data; // 这里是关键
    } catch (error) {
        console.log(error);
        throw error;
    }
}

export const deleteUser = async (row) => {
    try {
        WebUtils.addToken();
        const response =
            await axios.delete(`${apiUrl}/manage-user?userId=` + row.userId);
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        console.log(error);
        throw error;
    }
}

export const updateUser = async (row) => {
    try {
        const response =
            await axios.put(`${apiUrl}/manage-user?userId=` + row.userId +
                `&userState=` + (row.userState === 1 ? 0 : 1))
        WebUtils.isRedirect(response.data.code);
        return response.data;
    } catch (error) {
        console.log(error);
        throw error;
    }
}